using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pokemon
{
    class Charizard: Pokemon
    {
        private static Card _avatar;

        public static Card Avatar
        {
            get { return _avatar; }
            set { _avatar = value; }
        }

        public Charizard(Game game, Blank owner, Vector2 position) 
            : base(game, owner , position)
        {
        }

        public override void Initialize()
        {
            XDocument doc = XDocument.Load(@"Content/Textures/Pokemons/Charizard/CharizardDef.xml");
            Texture2D texture2 = null;
            Vector2 frameSize = Vector2.Zero;
            Vector2 centerPoint = Vector2.Zero;
            int numFrame;

            XName name = XName.Get(@"FrameWidth");            
            if (doc.Document != null)
            {
                var definitions = doc.Document.Descendants(name);
                Int32 frameWidth = Int32.Parse((definitions.First()).Value);
                
                name = XName.Get(@"FrameHeight");
                definitions = doc.Document.Descendants(name);
                Int32 frameHeight = Int32.Parse((definitions.First()).Value);
                _size = new Vector2(frameWidth, frameHeight);
                
                name = XName.Get("Damages");
                definitions = doc.Document.Descendants(name);
                _damage = Int32.Parse((definitions.First()).Value);
                
                name = XName.Get("Healths");
                definitions = doc.Document.Descendants(name);
                _health = Int32.Parse((definitions.First()).Value);
                
                name = XName.Get("Speed");
                definitions = doc.Document.Descendants(name);
                _bulledSpeed = Int32.Parse((definitions.First()).Value);

                //get range
                name = XName.Get("Range");
                definitions = doc.Document.Descendants(name);
                _range = Int32.Parse((definitions.First()).Value);

                //get delay time(s)
                name = XName.Get("Delay");
                definitions = doc.Document.Descendants(name);
                _delay = Int32.Parse((definitions.First()).Value); 

                name = XName.Get(@"Definition");
                definitions = doc.Document.Descendants(name);
                foreach (var definition in definitions)
                {
                    var xAttribute = definition.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        texture2 = Game.Content.Load<Texture2D>(xAttribute.Value);                        
                        xAttribute = definition.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = Int32.Parse(xAttribute.Value);
                            xAttribute = definition.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = Int32.Parse(xAttribute.Value);
                                frameSize = new Vector2(width, height);
                            }
                        }
                    }
                    xAttribute = definition.Attribute(@"XCenterPoint");

                    var xCenterPoint = Int32.Parse(xAttribute.Value);
                    xAttribute = definition.Attribute(@"YCenterPoint");

                    var yCenterPoint = Int32.Parse(xAttribute.Value);

                    centerPoint = new Vector2(xCenterPoint, yCenterPoint);

                    xAttribute = definition.Attribute(@"NumFrame");
                    numFrame = Int32.Parse(xAttribute.Value);

                    xAttribute = definition.Attribute(@"Name");
                    var type = xAttribute.Value;

                    Sprite sprite = new Sprite(texture2, numFrame, frameSize, centerPoint, false);

                    switch (type)
                    {
                        case "Idle":
                            Textures[0] = sprite;
                            break;
                        case "Attack":
                            Textures[1] = sprite;
                            break;
                        case "Hit":
                            Textures[2] = sprite;
                            break;
                        case "Dead":
                            Textures[3] = sprite;
                            break;
                        case "Stand":
                            Textures[4] = sprite;
                            break;
                    } 
                }

            }
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            _delayTimeSpan += gameTime.ElapsedGameTime;
            _TimeSpan += gameTime.ElapsedGameTime;
            if (_TimeSpan.Milliseconds < 100)
                return;

            //if (_TimeSpan.Milliseconds < 500 && _UnitState == UnitState.Attack && _frameIndex == 2)
            //{
            //    return;
            //}

            //if (_TimeSpan.Milliseconds >= 500 && _UnitState == UnitState.Attack && _frameIndex == 4)
            //{
            //    NextFrame();
            //    CharizardOnSkill();            
            //    return;
            //}

                         

            base.Update(gameTime);
            _TimeSpan = TimeSpan.Zero;

            // kiem tra xem tam ban cua Pokemon co phu muc tieu hay chua? va neu phu roi thi xem thoi gian delay giua 2 lan ban
            if (_enemy != null && Blank.GetDistance(GetCenterPoint(), _enemy.GetCenterPoint()) < _range && _delayTimeSpan.Seconds >= _delay)
            {
                // goi ham tan cong muc tieu
                Attack();
                // raise event ban ra dan
                Shot(_enemy);
                // gan thoi gian cho ban lai = 0
                _delayTimeSpan = TimeSpan.Zero;

            }
            if (UseSkill != null)
                OnUseSkill();

            Textures[GetUnitStateIndex()].Update();
        }

        public override void Draw(GameTime gameTime)
        {
            Rectangle rect = GetNextRect();
            SpriteBatch _SpriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            Textures[GetUnitStateIndex()].Draw(_SpriteBatch, _position, rect, _Color, 0.0f, 
                Vector2.Zero, 1.0f, SpriteEffects.FlipHorizontally, 0);
            base.Draw(gameTime);
        }

        public override void Shot(Unit target)
        {
            AddEvent();
            //base.Shot(target);
        }

        #region Event xu ly khi goi dan ban

        public delegate void UsingSkill();
        public event UsingSkill UseSkill;

        public void OnUseSkill()
        {
            UsingSkill hanlder = UseSkill;
            if (hanlder != null)
                hanlder();
        }

        public void CharizardOnSkill()
        {
            int index = GetUnitStateIndex();
            // neu nhu khung hinh dich chuyen den trang thai ban ra dan
            if ((_UnitState == UnitState.Attack) && (Textures[index].FrameIndex == 2 || Textures[index].FrameIndex == 4)) // && _UnitState == UnitState.Attack)
            {
                // tao ra dan 
                Fire b = new Fire(Game, this, _enemy);
                BulletManager t = (BulletManager)Game.Services.GetService(typeof(BulletManager));
                t.Add(b);
                // ban xong thi chuyen ve trang thai Idle
                if (Textures[index].FrameIndex >= 4)
                {
                    Idle();
                    RemoveEvent();
                }
            }            
        }

        public void AddEvent()//gan event vao de doi tuong biet la no dang ra skill
        {
            UseSkill += CharizardOnSkill;
        }

        public void RemoveEvent()
        {
            UseSkill -= CharizardOnSkill;
        }
        #endregion
    }
}
